﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace iPhoneBackupViewer
{
    public partial class ChooseBackupForm : Form
    {
        public ChooseBackupForm()
        {
            InitializeComponent();
        }

        private List<DirectoryInfo> backupDirs = new List<DirectoryInfo>();

        private void ChooseBackupForm_Load(object sender, EventArgs e)
        {
            DirectoryInfo backupRoot = new DirectoryInfo(Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                "Apple Computer", "MobileSync", "Backup"));

            foreach (DirectoryInfo dir in backupRoot.GetDirectories())
            {
                lstBackupFolders.Items.Add(GetBackupInfo(dir));
                backupDirs.Add(dir);
            }
            if (lstBackupFolders.Items.Count > 0)
                lstBackupFolders.SelectedIndex = 0;
        }

        private string GetBackupInfo(DirectoryInfo backupDir)
        {
            string filename = Path.Combine(backupDir.FullName, "Info.plist");
            var dict = (Dictionary<string, object>)PropertyList.Load(filename);
            return string.Format("{0} ({1})",
                dict["Display Name"], dict["Last Backup Date"]);
        }

        public DirectoryInfo SelectedDirectory
        {
            get
            {
                return lstBackupFolders.SelectedIndex >= 0 ?
                    backupDirs[lstBackupFolders.SelectedIndex] : null;
            }
        }
    }
}
